package com.myc.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myc.train.business.domain.TrainSeat;
import com.myc.train.business.domain.TrainStation;
import com.myc.train.common.resp.PageResp;
import com.myc.train.common.utils.SnowUtil;
import com.myc.train.business.domain.DailyTrainSeat;
import com.myc.train.business.domain.DailyTrainSeatExample;
import com.myc.train.business.mapper.DailyTrainSeatMapper;
import com.myc.train.business.req.DailyTrainSeatQueryReq;
import com.myc.train.business.req.DailyTrainSeatSaveReq;
import com.myc.train.business.resp.DailyTrainSeatQueryResp;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DailyTrainSeatService {

    @Resource
    private DailyTrainSeatMapper dailyTrainSeatMapper;

    @Resource
    private TrainSeatService trainSeatService;
    @Resource
    private TrainStationService trainStationService;

    private static final Logger LOG = LoggerFactory.getLogger(DailyTrainService.class);
    public void save(DailyTrainSeatSaveReq req){
        DateTime now = DateTime.now();
        DailyTrainSeat dailyTrainSeat = BeanUtil.copyProperties(req, DailyTrainSeat.class);

        if(ObjectUtil.isNull(dailyTrainSeat.getId())){
            dailyTrainSeat.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainSeat.setCreateTime(now);
            dailyTrainSeat.setUpdateTime(now);
            dailyTrainSeatMapper.insert(dailyTrainSeat);
        }else{
            dailyTrainSeat.setUpdateTime(now);
            dailyTrainSeatMapper.updateByPrimaryKey(dailyTrainSeat);
        }
    }

    public PageResp<DailyTrainSeatQueryResp> queryList(DailyTrainSeatQueryReq req){
       //定义查询条件
        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
        //根据id倒序排序
        dailyTrainSeatExample.setOrderByClause("date desc,train_code asc , carriage_index asc , carriage_seat_index asc");
        //创建查询对象
        DailyTrainSeatExample.Criteria criteria = dailyTrainSeatExample.createCriteria();
        //不为空则对其新增查询条件
        if(ObjectUtil.isNotEmpty(req.getTrainCode())){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }

        //新增分页信息
        PageHelper.startPage(req.getPage(),req.getSize());
        //根据条件查询结果
        List<DailyTrainSeat> dailyTrainSeats = dailyTrainSeatMapper.selectByExample(dailyTrainSeatExample);
        //new一个PageInfo并将结果放进来
        PageInfo<DailyTrainSeat> dailyTrainSeatPageInfo = new PageInfo<>(dailyTrainSeats);
        //类拷贝
        List<DailyTrainSeatQueryResp> queryResps = BeanUtil.copyToList(dailyTrainSeats, DailyTrainSeatQueryResp.class);
        //new一个PageResp
        PageResp<DailyTrainSeatQueryResp> resp= new PageResp<>();
        //放数据
        resp.setTotal(dailyTrainSeatPageInfo.getTotal());
        resp.setList(queryResps);


        return resp;
    }

    public void delete(Long id){
        dailyTrainSeatMapper.deleteByPrimaryKey(id);
    }

    //生成每日车座
    @Transactional
    public void genDailyTrain(Date date,String trainCode){
        DateTime now = DateTime.now();
        //删除原来当天改车次的座位
        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
        dailyTrainSeatExample.createCriteria().andDateEqualTo(date).andTrainCodeEqualTo(trainCode);
        dailyTrainSeatMapper.deleteByExample(dailyTrainSeatExample);
        //获取车座的售卖情况，根据数量补0
        List<TrainStation> trainStations = trainStationService.selectByCode(trainCode);
        String sell = StrUtil.fillBefore("",'0', trainStations.size()-1);

        //获取车座列表信息
        List<TrainSeat> trainSeats = trainSeatService.selectByTrainCode(trainCode);
        //非空校验
        if(CollUtil.isEmpty(trainSeats)){
            LOG.info("当前无车座信息");
        }
        //有数据则插入数据
        for(TrainSeat trainSeat: trainSeats){
            //拷贝到每日里
            DailyTrainSeat dailyTrainSeat = BeanUtil.copyProperties(trainSeat, DailyTrainSeat.class);
            dailyTrainSeat.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainSeat.setCreateTime(now);
            dailyTrainSeat.setUpdateTime(now);
            dailyTrainSeat.setDate(date);
            dailyTrainSeat.setSell(sell);
            dailyTrainSeatMapper.insert(dailyTrainSeat);
        }
    }

    //获取座位总数
    public int countSeat(Date date,String trainCode, String seatType){
        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
        dailyTrainSeatExample.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode)
                .andSeatTypeEqualTo(seatType);
        long l = dailyTrainSeatMapper.countByExample(dailyTrainSeatExample);
        if(l == 0L){
            l = -1;
        }
        return (int) l;

    }

    //获取该车厢下的座位列表
    public List<DailyTrainSeat> selectByCarriage(Date date, String trainCode, Integer carriageIndex){
        DailyTrainSeatExample example = new DailyTrainSeatExample();
        example.setOrderByClause("carriage_seat_index asc");
        example.createCriteria().andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode)
                .andCarriageIndexEqualTo(carriageIndex);
        return dailyTrainSeatMapper.selectByExample(example);
    }

}
